package com.example.restful01.controller;

import com.example.restful01.model.BorrowRecords;
import com.example.restful01.model.Reservation;
import com.example.restful01.service.ReservationService;
import com.example.restful01.service.UserService;
import com.example.restful01.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api") // 与前端axios的baseURL对应
@CrossOrigin // 跨域注解，也可以使用上面的全局配置
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    // 人员列表接口
    @GetMapping("/reservelist")
    public Result initBorrowslist() {
        if (!reservationService.list().isEmpty()) {
            return Result.success(reservationService.list());
        } else {
            return Result.error("查询所有用户失败");
        }
    }

    //人员删除接口
    @DeleteMapping("/cancelreserve")
    public Result deleteReserve(@RequestBody BorrowRecords borrowRecords) {
        if (reservationService.deleteReserve(borrowRecords)) {
            return Result.success("删除借阅记录成功");
        } else {
            return Result.error("删除借阅记录失败");
        }
    }

    @PostMapping("/addreserve")
    public Result addReserve(@RequestBody Reservation reservation) {
        if (reservationService.addReserve(reservation)) {
            return Result.success("添加预约记录成功");
        } else {
            return Result.error("添加预约记录失败");
        }
    }

    @GetMapping("/reserve")
    public Result initReserve(@RequestParam Integer readerId) {
        if (!reservationService.initReserve(readerId).isEmpty()) {
            return Result.success(reservationService.initReserve(readerId));
        }else {
            return Result.error("查询预约失败！");
        }
    }
}